<?php
include_once 'vendor/autoload.php';
use JPush\Model as JM;
use JPush\JPushClient;
use JPush\JPushLog;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

use JPush\Exception\APIConnectionException;
use JPush\Exception\APIRequestException;

class PushAction extends Action{
	public $client;	
	public function _initialize() {
		$app_key='cec0b5ebba2365f1659ea7b1';
		$master_secret ='9f077666f5820ef141793dc6';
		JPushLog::setLogHandlers(array(new StreamHandler('jpush.log', Logger::DEBUG)));
		$this->client = new JPushClient($app_key,$master_secret);
	}
	
	public function pushOneDevice(){
		$uid = $_REQUEST["uid"];
		if($uid == ""){
			$value = ["code" => "wrong"];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		$registration_id = M("Push_data")->where("uid = $uid")->find()["push_id"];
		$massage = isset($_REQUEST["massage"])?$_REQUEST["massage"]:"hello one";
		$br="/n";
		$audience = array();
		$platform = array("platform" => "all");
		try {
		    $result = $this->client->push()
		        ->setPlatform(JM\all)
		        ->setAudience(JM\alias(array($uid)))
		        ->setNotification(JM\notification($massage))
		        //->printJSON()
		        ->send();
		   $str =  'Push Success.' . $br;
		    $str .='sendno : ' . $result->sendno . $br;
		    $str .= 'msg_id : ' .$result->msg_id . $br;
		   $str .= 'Response JSON : ' . $result->json . $br;
		   
		   $value = ["code" => "success","massage" => $str];
		   header("Content-Type: text/json");
		   die(json_encode($value));		   
		} catch (APIRequestException $e) {
		    $str = 'Push Fail.' . $br;
		   $str .= 'Http Code : ' . $e->httpCode . $br;
		    $str .= 'code : ' . $e->code . $br;
		    $str .= 'Error Message : ' . $e->message . $br;
		   $str .= 'Response JSON : ' . $e->json . $br;
		    $str .= 'rateLimitLimit : ' . $e->rateLimitLimit . $br;
		    $str .= 'rateLimitRemaining : ' . $e->rateLimitRemaining . $br;
		    $str .= 'rateLimitReset : ' . $e->rateLimitReset . $br;
		    
		    $value = ["code" => "wrong","massage" => $str];
		    header("Content-Type: text/json");
		    die(json_encode($value));
		} catch (APIConnectionException $e) {
		   $str = 'Push Fail: ' . $br;
		   $str .= 'Error Message: ' . $e->getMessage() . $br;
		    //response timeout means your request has probably be received by JPUsh Server,please check that whether need to be pushed again.
		    $str .= 'IsResponseTimeout: ' . $e->isResponseTimeout . $br;
		    
		    $value = ["code" => "wrong","massage" => $str];
		    header("Content-Type: text/json");
		    die(json_encode($value));
		}
	}
	
	public function pushAllDevice(){
		
		$massage = isset($_REQUEST["massage"])?$_REQUEST["massage"]:"hello all";
		$br="/n";
		try {
			$result = $this->client->push()
			->setPlatform(JM\all)
			->setAudience(JM\all)
			->setNotification(JM\notification($massage))
			->printJSON()
			->send();
			$str =  'Push Success.' . $br;
			$str .='sendno : ' . $result->sendno . $br;
			$str .= 'msg_id : ' .$result->msg_id . $br;
			$str .= 'Response JSON : ' . $result->json . $br;
			 
			$value = ["code" => "success","massage" => $str];
			header("Content-Type: text/json");
			die(json_encode($value));
		} catch (APIRequestException $e) {
			$str = 'Push Fail.' . $br;
			$str .= 'Http Code : ' . $e->httpCode . $br;
			$str .= 'code : ' . $e->code . $br;
			$str .= 'Error Message : ' . $e->message . $br;
			$str .= 'Response JSON : ' . $e->json . $br;
			$str .= 'rateLimitLimit : ' . $e->rateLimitLimit . $br;
			$str .= 'rateLimitRemaining : ' . $e->rateLimitRemaining . $br;
			$str .= 'rateLimitReset : ' . $e->rateLimitReset . $br;
		
			$value = ["code" => "wrong","massage" => $str];
			header("Content-Type: text/json");
			die(json_encode($value));
		} catch (APIConnectionException $e) {
			$str = 'Push Fail: ' . $br;
			$str .= 'Error Message: ' . $e->getMessage() . $br;
			//response timeout means your request has probably be received by JPUsh Server,please check that whether need to be pushed again.
			$str .= 'IsResponseTimeout: ' . $e->isResponseTimeout . $br;
		
			$value = ["code" => "wrong","massage" => $str];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
	}
	
	public function pushServer(){
		$massage = "asdfasdfa";
		$this->pushAllDevicelocal($massage);
	}
	public function tete(){
		echo "asdfa";
	}
	public function pushAllDevicelocal($massage){
		$br="/n";
		try {
			$result = $this->client->push()
			->setPlatform(JM\all)
			->setAudience(JM\all)
			->setNotification(JM\notification($massage))
			->printJSON()
			->send();
// 			$str =  'Push Success.' . $br;
// 			$str .='sendno : ' . $result->sendno . $br;
// 			$str .= 'msg_id : ' .$result->msg_id . $br;
// 			$str .= 'Response JSON : ' . $result->json . $br;
		
// 			$value = ["code" => "success","massage" => $str];
// 			header("Content-Type: text/json");
// 			die(json_encode($value));
		} catch (APIRequestException $e) {
// 			$str = 'Push Fail.' . $br;
// 			$str .= 'Http Code : ' . $e->httpCode . $br;
// 			$str .= 'code : ' . $e->code . $br;
// 			$str .= 'Error Message : ' . $e->message . $br;
// 			$str .= 'Response JSON : ' . $e->json . $br;
// 			$str .= 'rateLimitLimit : ' . $e->rateLimitLimit . $br;
// 			$str .= 'rateLimitRemaining : ' . $e->rateLimitRemaining . $br;
// 			$str .= 'rateLimitReset : ' . $e->rateLimitReset . $br;
		
// 			$value = ["code" => "wrong","massage" => $str];
// 			header("Content-Type: text/json");
// 			die(json_encode($value));
		} catch (APIConnectionException $e) {
// 			$str = 'Push Fail: ' . $br;
// 			$str .= 'Error Message: ' . $e->getMessage() . $br;
// 			//response timeout means your request has probably be received by JPUsh Server,please check that whether need to be pushed again.
// 			$str .= 'IsResponseTimeout: ' . $e->isResponseTimeout . $br;
		
// 			$value = ["code" => "wrong","massage" => $str];
// 			header("Content-Type: text/json");
// 			die(json_encode($value));
		}
	}
	
	public function setPushId(){
	
		$data['uid'] =t($_REQUEST["uid"]);
		$data['channelId']=t($_REQUEST["push_id"]);
		$push = M("Push_data");
// 		dump($push);
		$push_data = $push->where("uid = ".$data['uid'])->find();
		if(!isset($push_data["uid"])){
// 			$push->uid = $data['uid'];
// 			$push->push_id = $data['channelId'];
			if($push->add($data)){
				$value = ["code" =>"success"];
				header("Content-Type: text/json");
				die(json_encode($value));
			}else{
				$value = ["code" =>"wrong","massage"=>"add".$push->getDbError()];
				header("Content-Type: text/json");
				die(json_encode($value));
			}
		}else{
			if($data["push_id"] == $data['channelId']){
				$value = ["code" =>"success"];
				header("Content-Type: text/json");
				die(json_encode($value));
			}
			$push->push_id = $data['channelId'];
	
			if($push->where("uid = ".$data['uid'])->save()){
				$value = ["code" =>"success"];
				header("Content-Type: text/json");
				die(json_encode($value));
			}else{
				$value = ["code" =>"wrong","massage"=>"save".$push->getDbError()];
				header("Content-Type: text/json");
				die(json_encode($value));
			}
		}
	}
	public function pushOneDeviceLocal($uid,$massage){
	
		if($uid == ""){
			$value = ["code" => "wrong"];
			
		}
		$registration_id = M("Push_data")->where("uid = $uid")->find()["push_id"];
		
		$br="/n";
		$audience = array();
		$platform = array("platform" => "all");
		try {
			$result = $this->client->push()
			->setPlatform(JM\all)
			->setAudience(JM\alias(array($uid)))
			->setNotification(JM\notification($massage))
			//->printJSON()
			->send();
			$str =  'Push Success.' . $br;
			$str .='sendno : ' . $result->sendno . $br;
			$str .= 'msg_id : ' .$result->msg_id . $br;
			$str .= 'Response JSON : ' . $result->json . $br;
			 
			$value = ["code" => "success","massage" => $str];
			header("Content-Type: text/json");
			die(json_encode($value));
		} catch (APIRequestException $e) {
			$str = 'Push Fail.' . $br;
			$str .= 'Http Code : ' . $e->httpCode . $br;
			$str .= 'code : ' . $e->code . $br;
			$str .= 'Error Message : ' . $e->message . $br;
			$str .= 'Response JSON : ' . $e->json . $br;
			$str .= 'rateLimitLimit : ' . $e->rateLimitLimit . $br;
			$str .= 'rateLimitRemaining : ' . $e->rateLimitRemaining . $br;
			$str .= 'rateLimitReset : ' . $e->rateLimitReset . $br;
	
			$value = ["code" => "wrong","massage" => $str];
			header("Content-Type: text/json");
			die(json_encode($value));
		} catch (APIConnectionException $e) {
			$str = 'Push Fail: ' . $br;
			$str .= 'Error Message: ' . $e->getMessage() . $br;
			//response timeout means your request has probably be received by JPUsh Server,please check that whether need to be pushed again.
			$str .= 'IsResponseTimeout: ' . $e->isResponseTimeout . $br;
	
			$value = ["code" => "wrong","massage" => $str];
			
		}
		return $value;
	}
}